/*   1:    */ package org.jetbrains.io;
/*   2:    */ 
/*   3:    */

import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ex.ApplicationInfoEx;
import com.intellij.openapi.util.text.StringUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http.HttpHeaders.Names;
import io.netty.util.CharsetUtil;
import org.jetbrains.annotations.Nullable;

import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */
/*  32:    */ 
/*  33:    */ public final class Responses
/*  34:    */ {
/*  35: 43 */   static final ThreadLocal<DateFormat> DATE_FORMAT = new ThreadLocal()
/*  36:    */   {
/*  37:    */     protected DateFormat initialValue()
/*  38:    */     {
/*  39: 47 */       SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
/*  40: 48 */       localSimpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
/*  41: 49 */       return localSimpleDateFormat;
/*  42:    */     }
/*  43:    */   };
/*  44:    */   private static String a;
/*  45:    */   
/*  46:    */   public static FullHttpResponse response(HttpResponseStatus paramHttpResponseStatus)
/*  47:    */   {
/*  48: 56 */     return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, paramHttpResponseStatus, Unpooled.EMPTY_BUFFER);
/*  49:    */   }
/*  50:    */   
/*  51:    */   public static HttpResponse response(@Nullable String paramString, @Nullable ByteBuf paramByteBuf)
/*  52:    */   {
/*  53: 60 */     DefaultFullHttpResponse localDefaultFullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, paramByteBuf == null ? Unpooled.EMPTY_BUFFER : paramByteBuf);
/*  54: 62 */     if (paramString != null) {
/*  55: 63 */       localDefaultFullHttpResponse.headers().add(HttpHeaders.Names.CONTENT_TYPE, paramString);
/*  56:    */     }
/*  57: 65 */     return localDefaultFullHttpResponse;
/*  58:    */   }
/*  59:    */   
/*  60:    */   public static void addAllowAnyOrigin(HttpResponse paramHttpResponse)
/*  61:    */   {
/*  62: 69 */     paramHttpResponse.headers().add(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
/*  63:    */   }
/*  64:    */   
/*  65:    */   public static void addDate(HttpResponse paramHttpResponse)
/*  66:    */   {
/*  67: 73 */     if (!paramHttpResponse.headers().contains(HttpHeaders.Names.DATE)) {
/*  68: 74 */       addDate(paramHttpResponse, Calendar.getInstance().getTime());
/*  69:    */     }
/*  70:    */   }
/*  71:    */   
/*  72:    */   public static void addDate(HttpResponse paramHttpResponse, Date paramDate)
/*  73:    */   {
/*  74: 79 */     paramHttpResponse.headers().set(HttpHeaders.Names.DATE, ((DateFormat)DATE_FORMAT.get()).format(paramDate));
/*  75:    */   }
/*  76:    */   
/*  77:    */   public static void addNoCache(HttpResponse paramHttpResponse)
/*  78:    */   {
/*  79: 83 */     paramHttpResponse.headers().add(HttpHeaders.Names.CACHE_CONTROL, "no-cache, no-store, must-revalidate, max-age=0");
/*  80: 84 */     paramHttpResponse.headers().add(HttpHeaders.Names.PRAGMA, "no-cache");
/*  81:    */   }
/*  82:    */   
/*  83:    */   @Nullable
/*  84:    */   public static String getServerHeaderValue()
/*  85:    */   {
/*  86: 89 */     if (a == null)
/*  87:    */     {
/*  88: 90 */       Application localApplication = ApplicationManager.getApplication();
/*  89: 91 */       if ((localApplication != null) && (!localApplication.isDisposed())) {
/*  90: 92 */         a = ApplicationInfoEx.getInstanceEx().getFullApplicationName();
/*  91:    */       }
/*  92:    */     }
/*  93: 95 */     return a;
/*  94:    */   }
/*  95:    */   
/*  96:    */   public static void addServer(HttpResponse paramHttpResponse)
/*  97:    */   {
/*  98: 99 */     if (getServerHeaderValue() != null) {
/*  99:100 */       paramHttpResponse.headers().add(HttpHeaders.Names.SERVER, getServerHeaderValue());
/* 100:    */     }
/* 101:    */   }
/* 102:    */   
/* 103:    */   public static void send(HttpResponse paramHttpResponse, Channel paramChannel, @Nullable HttpRequest paramHttpRequest)
/* 104:    */   {
/* 105:105 */     if ((paramHttpResponse.getStatus() != HttpResponseStatus.NOT_MODIFIED) && (!HttpHeaders.isContentLengthSet(paramHttpResponse))) {
/* 106:106 */       HttpHeaders.setContentLength(paramHttpResponse, (paramHttpResponse instanceof FullHttpResponse) ? ((FullHttpResponse)paramHttpResponse).content().readableBytes() : 0L);
/* 107:    */     }
/* 108:110 */     addCommonHeaders(paramHttpResponse);
/* 109:111 */     a(paramHttpResponse, paramChannel, (paramHttpRequest != null) && (!addKeepAliveIfNeed(paramHttpResponse, paramHttpRequest)));
/* 110:    */   }
/* 111:    */   
/* 112:    */   public static boolean addKeepAliveIfNeed(HttpResponse paramHttpResponse, HttpRequest paramHttpRequest)
/* 113:    */   {
/* 114:115 */     if (HttpHeaders.isKeepAlive(paramHttpRequest))
/* 115:    */     {
/* 116:116 */       HttpHeaders.setKeepAlive(paramHttpResponse, true);
/* 117:117 */       return true;
/* 118:    */     }
/* 119:119 */     return false;
/* 120:    */   }
/* 121:    */   
/* 122:    */   public static void addCommonHeaders(HttpResponse paramHttpResponse)
/* 123:    */   {
/* 124:123 */     addServer(paramHttpResponse);
/* 125:124 */     addDate(paramHttpResponse);
/* 126:125 */     addAllowAnyOrigin(paramHttpResponse);
/* 127:    */   }
/* 128:    */   
/* 129:    */   public static void send(CharSequence paramCharSequence, Channel paramChannel, @Nullable HttpRequest paramHttpRequest)
/* 130:    */   {
/* 131:129 */     send(paramCharSequence, CharsetUtil.US_ASCII, paramChannel, paramHttpRequest);
/* 132:    */   }
/* 133:    */   
/* 134:    */   public static void send(CharSequence paramCharSequence, Charset paramCharset, Channel paramChannel, @Nullable HttpRequest paramHttpRequest)
/* 135:    */   {
/* 136:133 */     send(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer(paramCharSequence, paramCharset)), paramChannel, paramHttpRequest);
/* 137:    */   }
/* 138:    */   
/* 139:    */   private static void a(HttpResponse paramHttpResponse, Channel paramChannel, boolean paramBoolean)
/* 140:    */   {
/* 141:137 */     if (!paramChannel.isActive()) {
/* 142:138 */       return;
/* 143:    */     }
/* 144:141 */     ChannelFuture localChannelFuture = paramChannel.write(paramHttpResponse);
/* 145:142 */     if (!(paramHttpResponse instanceof FullHttpResponse)) {
/* 146:143 */       paramChannel.write(LastHttpContent.EMPTY_LAST_CONTENT);
/* 147:    */     }
/* 148:145 */     paramChannel.flush();
/* 149:146 */     if (paramBoolean) {
/* 150:147 */       localChannelFuture.addListener(ChannelFutureListener.CLOSE);
/* 151:    */     }
/* 152:    */   }
/* 153:    */   
/* 154:    */   public static void sendStatus(HttpResponseStatus paramHttpResponseStatus, Channel paramChannel)
/* 155:    */   {
/* 156:152 */     sendStatus(paramHttpResponseStatus, paramChannel, null);
/* 157:    */   }
/* 158:    */   
/* 159:    */   public static void sendStatus(HttpResponseStatus paramHttpResponseStatus, Channel paramChannel, @Nullable HttpRequest paramHttpRequest)
/* 160:    */   {
/* 161:156 */     sendStatus(paramHttpResponseStatus, paramChannel, null, paramHttpRequest);
/* 162:    */   }
/* 163:    */   
/* 164:    */   public static void sendStatus(HttpResponseStatus paramHttpResponseStatus, Channel paramChannel, @Nullable String paramString, @Nullable HttpRequest paramHttpRequest)
/* 165:    */   {
/* 166:160 */     send(a(paramHttpResponseStatus, paramHttpRequest, paramString), paramChannel, paramHttpRequest);
/* 167:    */   }
/* 168:    */   
/* 169:    */   private static HttpResponse a(HttpResponseStatus paramHttpResponseStatus, @Nullable HttpRequest paramHttpRequest, @Nullable String paramString)
/* 170:    */   {
/* 171:164 */     if ((paramHttpRequest != null) && (paramHttpRequest.getMethod() == HttpMethod.HEAD)) {
/* 172:165 */       return response(paramHttpResponseStatus);
/* 173:    */     }
/* 174:168 */     StringBuilder localStringBuilder = new StringBuilder();
/* 175:169 */     String str = paramHttpResponseStatus.toString();
/* 176:170 */     localStringBuilder.append("<!doctype html><title>").append(str).append("</title>").append("<h1 style=\"text-align: center\">").append(str).append("</h1>");
/* 177:171 */     if (paramString != null) {
/* 178:172 */       localStringBuilder.append("<p>").append(paramString).append("</p>");
/* 179:    */     }
/* 180:174 */     localStringBuilder.append("<hr/><p style=\"text-align: center\">").append(StringUtil.notNullize(getServerHeaderValue(), "")).append("</p>");
/* 181:    */     
/* 182:176 */     DefaultFullHttpResponse localDefaultFullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, paramHttpResponseStatus, Unpooled.copiedBuffer(localStringBuilder, CharsetUtil.UTF_8));
/* 183:177 */     localDefaultFullHttpResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html");
/* 184:178 */     return localDefaultFullHttpResponse;
/* 185:    */   }
/* 186:    */   
/* 187:    */   public static void sendOptionsResponse(String paramString, HttpRequest paramHttpRequest, ChannelHandlerContext paramChannelHandlerContext)
/* 188:    */   {
/* 189:182 */     FullHttpResponse localFullHttpResponse = response(HttpResponseStatus.OK);
/* 190:183 */     localFullHttpResponse.headers().set(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_METHODS, paramString);
/* 191:184 */     localFullHttpResponse.headers().set(HttpHeaders.Names.ALLOW, paramString);
/* 192:185 */     send(localFullHttpResponse, paramChannelHandlerContext.channel(), paramHttpRequest);
/* 193:    */   }
/* 194:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.io.Responses

 * JD-Core Version:    0.7.0.1

 */